package com.shxt.framework.rbac.user.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shxt.framework.commons.exception.LoginAccountExceiption;
import com.shxt.framework.commons.exception.ServiceExceiption;
import com.shxt.framework.commons.model.Pager;
import com.shxt.framework.commons.util.MD5Helper;
import com.shxt.framework.rbac.user.dao.UserDao;
import com.shxt.framework.rbac.user.model.User;
import com.shxt.framework.rbac.user.service.UserService;
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;
	@Override
	public User login( String account , String password ) {
		//获取用户信息
		User  user = this.userDao.login(account, MD5Helper.MD5(password, 1024));
		if(user==null) {
			throw new LoginAccountExceiption("账号或者密码错误,请重新登录");
		}else {
			//获取用户的状态
			int status= user.getStatus();
			if(status==0) {
				throw new ServiceExceiption("该账号已经被注销,请联系管理员");
			}
			return user;
		}
	}

	@Override
	public Pager find( Pager pager ) {
		pager.setDatas(this.userDao.getUserList(pager));
		pager.setTotalCount(this.userDao.getUserCount(pager));
		return pager;
	}

	@Override
	public void add( User user ) {
		user.setPassword(MD5Helper.MD5(user.getPassword(), 1024));
		this.userDao.add(user);
	}

	@Override
	public void validUserAccount( String account ) {
		int count = this.userDao.getUserAccountCount(account);
		if (count>0) {
			//下面这句话，通过抛出异常，来控制流程
			throw new ServiceExceiption("该账号已经被注册");
		}
	}

	@Override
	public User load( Integer user_id ) {
		return this.userDao.load(user_id);
	}

	@Override
	public void update( User user ) {
		this.userDao.update(user);
	}

	@Override
	public void updateStatus( User user ) {
		try {
			this.userDao.updateUserStatus(user);
		} catch (Exception ex) {
			ex.printStackTrace();//给开发人员使用
			throw new ServiceExceiption("更新用户的状态失败");
		}
	}

	@Override
	public void validUserPassword( String password , Integer user_id ) {
		int count = this.userDao.getUserPasswordCount(MD5Helper.MD5(password, 1024), user_id);
		if (count==0) {
			//下面这句话，通过抛出异常，来控制流程
			throw new ServiceExceiption("您输入的密码错误");
		}
	}

}
